/**
 * Created by TEANA on 2017/6/15.
 */

module.exports = contentModule => {
  contentModule.controller('contentCtrl',
    function($rootScope,
             $scope,
             $state,
             $log,
             $stateParams,
             $timeout,
             $ionicScrollDelegate,
             $location,
             $filter,
             $native,
             Argument,
             Config,
             Dom,
             contentFactory) {

      $scope.$on('$ionicView.afterEnter', function() {
        $native.setNavigation($scope.vo.title , 0);
      });
      $scope.$on('$ionicView.beforeLeave', function() {
        var segmentation = {
          infoCode:$stateParams.infoCode,
          stocks:($scope.vo.content?$scope.vo.content.stockList:'')
        }
        $native.endEvent(Config.contentKey, JSON.stringify(segmentation));
        //@TODO: 隐藏下载弹出浮层
        if($rootScope.args.autoload === '0'){
          var node = document.getElementsByClassName('fz_download')[0];
          Dom.removeClass(node, 'show');
        }
      });

      $scope.$watch('$viewContentLoaded', function () {
        $scope.vc.init();
        $scope.vo.width = ($rootScope.screenWidth - 46) +'px';
      });
      $scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) {
        var stocksNode = document.querySelector('.fusc-info-stocks-container');
        var imageNode = document.querySelector('.fusc-info-cont-img-container');
        var stocksHeight = stocksNode.offsetHeight;
        if(stocksHeight > 60){
          stocksNode.style.maxHeight = 60 + 'px';
          imageNode.classList.add('show');
        }
        $scope.vo.liHeight = stocksNode.style.maxHeight;
      });

      // 初始化设置加载状态
      $scope.vo = {
        goBack:false,
        contentFontSizeClass:''  // 字体大小
      };

      //页面状态
      $scope.status = Config.status.init;
      $scope.vo.title = '新闻';
      $scope.vc = {
        // 初始化-首次获取 资讯数据
        init:function () {
          $scope.status = Config.status.loading;
          // 设置默认字体大小
          var fontOpts = ['fusc-ft-normal', 'fusc-ft-big', 'fusc-ft-large'];
          $scope.vo.contentFontSizeClass = fontOpts[$native.getFontSize()];

          if($stateParams.infoCode){
            var promise = contentFactory.get($stateParams.infoCode);
            promise.then(function (response) {
              if(response.infoCode){
                $scope.vo.content = response;
                $native.setShare($scope.vc.getShareParams($scope.vo.content));
                $scope.status = Config.status.success;
              }else {
                $scope.status = Config.status.noData;
              }
            }, function (error) {
              $scope.status = Config.status.timeout;
            }).finally(function() {
              //@TODO: 显示下载弹出浮层
              if(($scope.status === Config.status.success) &&!$native.isNative() ){
                var node = document.getElementsByClassName('fz_download')[0];
                Dom.addClass(node, 'show');
              }
            });
          }else{
            $state.go('main');
          }
        },
        doSwiper:function () {
          var contentScroll = $ionicScrollDelegate.$getByHandle('contentScroll');
          var top = parseInt(contentScroll.getScrollPosition().top);
          var goTopBtns = document.getElementsByClassName('fusc-go-top');
          var goTopBtn = goTopBtns[goTopBtns.length - 1];

          // 显示和隐藏返回顶部弹层
          if(goTopBtn){
            if(top > 0){
              goTopBtn.setAttribute('style', 'opacity: 1');
            }else{
              goTopBtn.setAttribute('style', 'opacity: 0');
            }
          }
        },
        goTop:function () {
          // 显示和隐藏返回顶部弹层
          var goTopBtn = document.getElementById('contentGoTop');
          goTopBtn.setAttribute('style', 'opacity: 0');
          var contentScroll = $ionicScrollDelegate.$getByHandle('contentScroll');
          contentScroll.scrollTop(true);
        },
        goBack:function ($event) {
          if($scope.vo.goBack){
            return;
          }
          if($event.gesture.deltaX > $rootScope.gestureGap){
            $scope.vo.goBack = true;
            window.history.back();
          }
        },
        toggle:function () {
          var stocksNode = document.querySelector('.fusc-info-stocks-container');
          var expNode = document.querySelector('.fusc-cont-stocks-expand');
          var image = expNode.getAttribute('src');
          if('img/icon-expend.png' === image){//展开
            stocksNode.style.maxHeight = 'none';
            expNode.setAttribute('src', 'img/icon-packup.png');
            $scope.vo.liHeight = stocksNode.offsetHeight + 'px';
          }else{//关闭
            expNode.setAttribute('src', 'img/icon-expend.png');
            stocksNode.style.maxHeight = '60px';
            $scope.vo.liHeight = stocksNode.style.maxHeight;
          }
        },
        showStockDetail:function (stockCode) {
          $native.showStockDetail(stockCode);
        },
        //获取分享参数
        getShareParams:function (infoVo) {
          var params = {title:'', content:''};
          if(infoVo){
            params.title = infoVo.title;
            if(infoVo.content){
              var content = $filter('htmlTagDel')(infoVo.content);
              params.content = $filter('textLengthSet')(content, true, 100);
            }
          }
          return JSON.stringify(params);
        }
      };

    })
};
